cmake_minimum_required(VERSION 3.10)
project(game_server)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# Find required packages
find_package(libwebsockets REQUIRED)
find_package(nlohmann_json REQUIRED)

# Find MySQL Connector/C++
find_path(MYSQLCPPCONN_INCLUDE_DIR
    NAMES mysql_connection.h
    PATH_SUFFIXES cppconn
    PATHS
    /usr/include/mysql-cppconn-8
    /usr/include/mysql-cppconn-8/mysql
    /usr/include/mysql
    /usr/local/include/mysql
)

find_library(MYSQLCPPCONN_LIBRARY
    NAMES mysqlcppconn
    PATHS
    /usr/lib
    /usr/local/lib
)

if(NOT MYSQLCPPCONN_INCLUDE_DIR OR NOT MYSQLCPPCONN_LIBRARY)
    message(FATAL_ERROR "MySQL Connector/C++ not found. Please install libmysqlcppconn-dev package.")
endif()

# Find MySQL client library (needed for some symbols)
find_library(MYSQLCLIENT_LIBRARY
    NAMES mysqlclient
    PATHS
    /usr/lib
    /usr/local/lib
)

if(NOT MYSQLCLIENT_LIBRARY)
    message(FATAL_ERROR "MySQL client library not found. Please install libmysqlclient-dev package.")
endif()

# Find hiredis
find_path(HIREDIS_INCLUDE_DIR
    NAMES hiredis/hiredis.h
    PATH_SUFFIXES include
)

find_library(HIREDIS_LIBRARY
    NAMES hiredis
)

if(NOT HIREDIS_INCLUDE_DIR OR NOT HIREDIS_LIBRARY)
    message(FATAL_ERROR "hiredis not found. Please install hiredis development libraries.")
endif()

# Find other packages
find_package(spdlog REQUIRED)

# Add subdirectories
add_subdirectory(src)

# Set include directories
include_directories(
    ${CMAKE_SOURCE_DIR}/include
    ${LIBWEBSOCKETS_INCLUDE_DIRS}
    ${MYSQLCPPCONN_INCLUDE_DIR}
    ${HIREDIS_INCLUDE_DIR}
)

# Set output directories
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)

# Print found packages information
message(STATUS "Found MySQL Connector/C++: ${MYSQLCPPCONN_LIBRARY}")
message(STATUS "MySQL Connector/C++ include dir: ${MYSQLCPPCONN_INCLUDE_DIR}")
message(STATUS "Found hiredis: ${HIREDIS_LIBRARY}")
message(STATUS "hiredis include dir: ${HIREDIS_INCLUDE_DIR}")
message(STATUS "libwebsockets include dirs: ${LIBWEBSOCKETS_INCLUDE_DIRS}") 